package cn.iocoder.yudao.module.blog.controller.admin.postcomment;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.blog.controller.admin.postcomment.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.postcomment.BlogPostCommentDO;
import cn.iocoder.yudao.module.blog.convert.postcomment.BlogPostCommentConvert;
import cn.iocoder.yudao.module.blog.service.postcomment.BlogPostCommentService;

@Tag(name = "管理后台 - 帖子评论")
@RestController
@RequestMapping("/blog/post-comment")
@Validated
public class BlogPostCommentController {

    @Resource
    private BlogPostCommentService postCommentService;

    @PostMapping("/create")
    @Operation(summary = "创建帖子评论")
    @PreAuthorize("@ss.hasPermission('blog:post-comment:create')")
    public CommonResult<Long> createPostComment(@Valid @RequestBody BlogPostCommentCreateReqVO createReqVO) {
        return success(postCommentService.createPostComment(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新帖子评论")
    @PreAuthorize("@ss.hasPermission('blog:post-comment:update')")
    public CommonResult<Boolean> updatePostComment(@Valid @RequestBody BlogPostCommentUpdateReqVO updateReqVO) {
        postCommentService.updatePostComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除帖子评论")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blog:post-comment:delete')")
    public CommonResult<Boolean> deletePostComment(@RequestParam("id") Long id) {
        postCommentService.deletePostComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得帖子评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blog:post-comment:query')")
    public CommonResult<BlogPostCommentRespVO> getPostComment(@RequestParam("id") Long id) {
        BlogPostCommentDO postComment = postCommentService.getPostComment(id);
        return success(BlogPostCommentConvert.INSTANCE.convert(postComment));
    }

    @GetMapping("/list")
    @Operation(summary = "获得帖子评论列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blog:post-comment:query')")
    public CommonResult<List<BlogPostCommentRespVO>> getPostCommentList(@RequestParam("ids") Collection<Long> ids) {
        List<BlogPostCommentDO> list = postCommentService.getPostCommentList(ids);
        return success(BlogPostCommentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得帖子评论分页")
    @PreAuthorize("@ss.hasPermission('blog:post-comment:query')")
    public CommonResult<PageResult<BlogPostCommentRespVO>> getPostCommentPage(@Valid BlogPostCommentPageReqVO pageVO) {
        PageResult<BlogPostCommentDO> pageResult = postCommentService.getPostCommentPage(pageVO);
        return success(BlogPostCommentConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出帖子评论 Excel")
    @PreAuthorize("@ss.hasPermission('blog:post-comment:export')")
    @OperateLog(type = EXPORT)
    public void exportPostCommentExcel(@Valid BlogPostCommentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<BlogPostCommentDO> list = postCommentService.getPostCommentList(exportReqVO);
        // 导出 Excel
        List<BlogPostCommentExcelVO> datas = BlogPostCommentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "帖子评论.xls", "数据", BlogPostCommentExcelVO.class, datas);
    }

}
